package com.ruoyi.web.controller.bl;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.bl.domain.BlBrand;
import com.ruoyi.bl.service.IBlBrandService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bl.domain.BlSuppliers;
import com.ruoyi.bl.service.IBlSuppliersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商Controller
 * 
 * @author ruoyi
 * @date 2023-08-20
 */
@RestController
@RequestMapping("/bl/suppliers")
public class BlSuppliersController extends BaseController
{
    @Autowired
    private IBlSuppliersService blSuppliersService;
    @Autowired
    private IBlBrandService blBrandService;

    /**
     * 查询供应商列表
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlSuppliers blSuppliers)
    {
        startPage();
        List<BlSuppliers> list = blSuppliersService.selectBlSuppliersList(blSuppliers);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:export')")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlSuppliers blSuppliers)
    {
        List<BlSuppliers> list = blSuppliersService.selectBlSuppliersList(blSuppliers);
        ExcelUtil<BlSuppliers> util = new ExcelUtil<BlSuppliers>(BlSuppliers.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:query')")
    @GetMapping(value = { "/", "/{supplierId}" })
    public AjaxResult getInfo(@PathVariable(value = "supplierId", required = false) Long supplierId)
    {
        AjaxResult ajax = AjaxResult.success();
        List<BlBrand> blBrands=blBrandService.selectBlBrandList(new BlBrand());
        ajax.put("blBrands",blBrands);
        if (StringUtils.isNotNull(supplierId)) {
            ajax.put("blSuppliers",blSuppliersService.selectBlSuppliersBySupplierId(supplierId));
        }
        return ajax;
    }

    /**
     * 新增供应商
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:add')")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlSuppliers blSuppliers)
    {
        blSuppliers.setCreateBy(getUsername());
        return toAjax(blSuppliersService.insertBlSuppliers(blSuppliers));
    }

    /**
     * 修改供应商
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:edit')")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlSuppliers blSuppliers)
    {
        blSuppliers.setUpdateBy(getUsername());
        return toAjax(blSuppliersService.updateBlSuppliers(blSuppliers));
    }

    /**
     * 删除供应商
     */
    @PreAuthorize("@ss.hasPermi('bl:suppliers:remove')")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
	@DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        return toAjax(blSuppliersService.deleteBlSuppliersBySupplierIds(supplierIds));
    }
}
